package cn.waqwb.bwcar.controller;

import cn.waqwb.bwcar.dto.DataGridResult;
import cn.waqwb.bwcar.dto.QueryDTO;
import cn.waqwb.bwcar.pojo.CarMake;
import cn.waqwb.bwcar.pojo.CarModel;
import cn.waqwb.bwcar.service.CarMakeService;
import cn.waqwb.bwcar.service.CarModelService;
import cn.waqwb.bwcar.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author JokerQ
 * @date 2019/11/25 - 14:58
 */
@RestController
public class CarModelController {
    @Autowired
    private CarModelService carModelService;
    @Autowired
    private CarMakeService carMakeService;


    @RequestMapping("/sys/model/list")
    public DataGridResult findCarPage(QueryDTO queryDTO){
        return carModelService.findByPage(queryDTO);
    }


    @RequestMapping("/sys/model/del")
    public R deleteCarModel(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            carModelService.delCarModel(id);
        }
        return R.ok();
    }


    @RequestMapping("/sys/model/typeall")
    public R typeAll(){
        List<CarMake> all = carMakeService.findAll();
        return R.ok().put("sites",all);
    }


    @RequestMapping("/sys/model/info/{id}")
    public R findById(@PathVariable("id") Integer id){
        CarModel byId = carModelService.findById(id);
        return R.ok().put("model",byId);
    }


    @RequestMapping("/sys/model/save")
    public R addCarModel(@RequestBody CarModel carModel){
        int i = carModelService.addCarModel(carModel);
        return i>0?R.ok():R.error("添加失败");
    }


    @RequestMapping("/sys/model/update")
    public R updateCarModel(@RequestBody CarModel carModel){
        int i = carModelService.updateCarModel(carModel);
        return i>0?R.ok():R.error("修改失败");
    }

}
